<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <title>RSA Password Encrypt Tool</title>
  <script src="https://cdn.jsdelivr.net/npm/js-base64@3.7.5/base64.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/jsencrypt@3.2.1/bin/jsencrypt.min.js"></script>
  <style>
    body {
      font-family: Arial, sans-serif;
      padding: 2em;
    }

    input,
    textarea,
    button {
      width: 100%;
      padding: 0.5em;
      margin-top: 1em;
    }
  </style>
</head>

<body>
  <h2>RSA + Base64 密码加密工具</h2>

  <label for="password">输入密码：</label>
  <input type="text" id="password" placeholder="请输入密码" />

  <button onclick="encryptPassword()">加密</button>

  <label for="result">加密后的结果</label>
  <textarea id="result" rows="6" readonly></textarea>

  <script>
    const pubKey = `-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArq9XTUSeYr2+N1h3Afl/
z8Dse/2yD0ZGrKwx+EEEcdsBLca9Ynmx3nIB5obmLlSfmskLpBo0UACBmB5rEjBp
2Q2f3AG3Hjd4B+gNCG6BDaawuDlgANIhGnaTLrIqWrrcm4EMzJOnAOI1fgzJRsOO
UEfaS318Eq9OVO3apEyCCt0lOQK6PuksduOjVxtltDav+guVAA068NrPYmRNabVK
RNLJpL8w4D44sfth5RvZ3q9t+6RTArpEtc5sh5ChzvqPOzKGMXW83C95TxmXqpbK
6olN4RevSfVjEAgCydH6HN6OhtOQEcnrU97r9H0iZOWwbw3pVrZiUkuRD1R56Wzs
2wIDAQAB
-----END PUBLIC KEY-----`;

    function encryptPassword() {
      const password = document.getElementById('password').value;
      if (!password) {
        alert('请输入密码');
        return;
      }

      const encryptor = new JSEncrypt();
      encryptor.setPublicKey(pubKey);

      const base64Password = Base64.encode(password);
      const encrypted = encryptor.encrypt(base64Password);

      document.getElementById('result').value = encrypted || '加密失败，请检查密钥或输入';
    }
  </script>
</body>

</html>
